#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct Stack {
    int top;
    int capacity;
    int *arr;
} Stack;

Stack *initStack(int high) {
    Stack *stack = (Stack *)malloc(sizeof(Stack));
    stack->top = -1;
    stack->capacity = high;
    stack->arr = (int *)malloc(high * sizeof(int));
    return stack;
}

bool isEmpty(Stack *stack) { return stack->top == -1; }

bool isFull(Stack *stack) { return stack->top == stack->capacity - 1; }

void push(Stack *stack, int data) {
    if (isFull(stack)) {
        perror("Stack is full.\n");
        exit(EXIT_FAILURE);
    }
    stack->arr[++stack->top] = data;
    printf("%d入栈\n", data);
}

int pop(Stack *stack) {
    if (isEmpty(stack)) {
        perror("Stack is empty.\n");
        exit(EXIT_FAILURE);
    }
    return stack->arr[stack->top--];
}

void printStack(Stack *stack) {
    if (!isEmpty(stack)) {
        for (int i = 0; i <= stack->top; i++) {
            printf("%d ", stack->arr[i]);
        }
    }
    printf("\n");
}

void destroyStack(Stack *stack) {
    free(stack->arr);
    free(stack);
}

int main() {
    Stack *stack = initStack(5);
    push(stack, 1);
    push(stack, 2);
    push(stack, 3);
    push(stack, 4);
    push(stack, 5);
    // push(stack, 6);
    printStack(stack);
    printf("%d出栈\n", pop(stack));
    printf("%d出栈\n", pop(stack));
    printf("%d出栈\n", pop(stack));
    printf("%d出栈\n", pop(stack));
    printf("%d出栈\n", pop(stack));
    destroyStack(stack);
}